<script setup>
import {
  getBizCouponInfo,
  addBizCouponInfo,
  updateBizCouponInfo,
  findMaxSort
} from "@/api/biz/bizCouponInfo";
import storage from "@/utils/storage";
import { TOKEN } from "@/store/mutation-types";
const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});
const fileHeahers = ref({
  url: import.meta.env.VITE_APP_API_BASEURL + "/common/upload",
  headers: {
    Authorization: "Bearer " + storage.local.get(TOKEN)
  }
});
function handleAvatarSuccess(res, file) {
  data.value.form.image = file.response.data.url;
}
const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id,
    type: "0",
    status: "0"
  },
  rules: {
    name: [
      { required: true, message: "请输入登陆账号", trigger: "blur" },
      { validator: getcheckUserNameUnique }
    ]
  }
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort().then((res) => {
      data.value.form.sort = res.data;
    });
  }
});

function getInfo() {
  data.value.loading = true;
  getBizCouponInfo(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

function getcheckUserNameUnique(rule, value, callback) {
  if (data.value.form.id !== "" || data.value.form.id !== undefined) {
    callback();
  }
  const msg = "登陆账号已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      userName: value
    };
    checkUserNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addBizCouponInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizCouponInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input placeholder="请输入姓名" v-model="data.form.title" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="image">
            <el-upload
              class="avatar-uploader"
              :action="fileHeahers.url"
              :headers="fileHeahers.headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img
                v-if="data.form.image"
                :src="data.form.image"
                class="avatar"
              />

              <svg-icon
                v-else
                class="el-icon-plus avatar-uploader-icon"
                name="ep:picture-rounded"
              />
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优惠券金额" prop="amount">
            <el-input-number
              placeholder="优惠券金额"
              v-model="data.form.amount"
              :precision="2"
              :step="1"
              :max="1000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="满减" prop="amount">
            <el-input-number
              placeholder="满减"
              v-model="data.form.fullSub"
              :precision="2"
              :step="1"
              :max="1000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="data.form.type">
              <el-radio-button label="0" checked="checked"
                >洗护券</el-radio-button
              >
              <el-radio-button label="1">商城券</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发行数量" prop="counts">
            <el-input-number
              placeholder="发行数量"
              v-model="data.form.counts"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="过期时间" prop="outTime">
            <el-date-picker
              v-model="data.form.outTime"
              type="datetime"
              placeholder="请选择过期时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="data.form.startTime"
              type="datetime"
              placeholder="请选择开始时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              v-model="data.form.endTime"
              type="datetime"
              placeholder="请选择结束时间"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="data.form.status">
              <el-radio-button label="0" checked="checked"
                >停用</el-radio-button
              >
              <el-radio-button label="1">启用</el-radio-button>
              <el-radio-button label="2">过期</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              placeholder="请输入备注"
              v-model="data.form.remark"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 240px;
  height: 80px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 38px;
  color: #409eff;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 240px;
  height: 80px;
  display: block;
}
</style>
